package sd.czw.com.mmgapp.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.zhy.http.okhttp.OkHttpUtils;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import sd.czw.com.mmgapp.Contants;
import sd.czw.com.mmgapp.R;
import sd.czw.com.mmgapp.mcallback.YHJCallback;
import sd.czw.com.mmgapp.model.personalbean.YHJModel;
import sd.czw.com.mmgapp.utils.SpUtils;

/**
 * Created by czw
 * desc 描述：
 * on 2017/7/14.
 */
public class YHJActivity extends AppCompatActivity {


    @Bind(R.id.rl_back)
    RelativeLayout rlBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.rl_right)
    RelativeLayout rlRight;
    @Bind(R.id.lv)
    ListView lv;
    private String token;
    private Map<String, String> params = new HashMap<>();
    private List<YHJModel.ErrmsgBean> mdatas = new ArrayList<>();
    private String username;
    private ListViewAdapter listViewAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yhj);
        ButterKnife.bind(this);
        username = SpUtils.setUsername(YHJActivity.this).getString("username", "");
        token = SpUtils.setToken(YHJActivity.this).getString("token", "");
        System.out.println("1:"+username);
        System.out.println("2:"+token);
        params.put("time", Contants.Time);
        params.put("sign", Contants.Sign);
        params.put("token", token);
        params.put("username", username);
        params.put("act", "bonus");
        getdatas();
        listViewAdapter = new ListViewAdapter();
        lv.setAdapter(listViewAdapter);
    }

    private void getdatas() {
        OkHttpUtils.post().url(Contants.YHJ).params(params).addParams("start", "0").addParams("limit", "5")
                .build().execute(new YHJCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {

            }

            @Override
            public void onResponse(YHJModel response, int id) {
                if (response.getErrcode() == 1) {
                    mdatas.addAll(response.getErrmsg());
                    System.out.println("length:"+mdatas.size());
                    listViewAdapter.notifyDataSetChanged();
                }
            }
        });
    }

    @OnClick({R.id.rl_back, R.id.rl_right})
    void onclick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                onBackPressed();
                break;
            case R.id.rl_right:
                break;
        }
    }


    class ListViewAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mdatas.size();
        }

        @Override
        public Object getItem(int i) {
            return mdatas.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder viewHolder;
            if (view == null){
                viewHolder = new ViewHolder();
                view = LayoutInflater.from(YHJActivity.this).inflate(R.layout.yhj_item,viewGroup,false);
                viewHolder.tvMoney = (TextView) view.findViewById(R.id.tv_money);
                viewHolder.title = (TextView) view.findViewById(R.id.title);
                viewHolder.qixian = (TextView) view.findViewById(R.id.qixian);
                viewHolder.dingd = (TextView) view.findViewById(R.id.dingd);
                viewHolder.startday = (TextView) view.findViewById(R.id.startday);
                viewHolder.endday = (TextView) view.findViewById(R.id.endday);
                view.setTag(viewHolder);
            }else {
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.tvMoney.setText("¥"+mdatas.get(i).getType_money());
            viewHolder.title.setText(mdatas.get(i).getType_name());
            viewHolder.startday.setText("起始日期:"+mdatas.get(i).getUse_startdate());
            viewHolder.startday.setText("结束日期:"+mdatas.get(i).getUse_enddate());
            viewHolder.dingd.setText("最小订单："+mdatas.get(i).getMin_goods_amount());
            viewHolder.qixian.setText(mdatas.get(i).getStatus());
            return view;
        }


        class ViewHolder {
            TextView tvMoney;
            TextView title;
            TextView qixian;
            TextView dingd;
            TextView startday;
            TextView endday;
            LinearLayout lvItem;
        }
    }
}
